import*as t from"../../core/common/common.js";import*as e from"../../core/host/host.js";import*as i from"../../core/i18n/i18n.js";import*as a from"../../core/sdk/sdk.js";import*as o from"../../ui/legacy/components/data_grid/data_grid.js";import*as n from"../../ui/legacy/legacy.js";const r=new CSSStyleSheet;r.replaceSync(".webauthn-pane{overflow:auto;min-width:500px}.webauthn-toolbar-container{display:flex;background-color:var(--color-background-elevation-1);border-bottom:var(--legacy-divider-border);flex:0 0 auto}.webauthn-toolbar{border-bottom:var(--legacy-divider-border);display:inline-block}.authenticators-view{margin:0 10px;min-height:auto;display:none}.webauthn-pane.enabled .authenticators-view{display:block}.new-authenticator-title{line-height:24px;font-weight:bold;display:block}.new-authenticator-container{display:none;margin:10px}.webauthn-pane.enabled .new-authenticator-container{display:block}.new-authenticator-form{border:none;padding:10px 0;flex:0 0 auto;margin:0}.webauthn-pane .chrome-select{width:120px}.authenticator-section{display:block;padding:16px 0;border-bottom:1px solid var(--color-details-hairline)}.authenticator-fields{border:none;padding:10px 0;flex:0 0 auto;margin:0}.authenticator-field{display:flex;margin:auto}.authenticator-section-header{display:flex;justify-content:space-between;align-items:flex-end}.authenticator-section-title{line-height:24px;display:inline-block}.authenticator-section-title .authenticator-name-field{display:inline-block;font-weight:bold;border:none;animation:save-flash 0.2s;text-overflow:ellipsis}.authenticator-section-title.editing-name .authenticator-name-field{border-bottom:1px solid var(--input-outline);font-weight:normal;animation:none}.authenticator-field-value{padding:5px 0;display:inline-block;font-family:monospace}.authenticator-option-checkbox{position:relative;top:2px}.authenticator-option{display:flex;padding-bottom:10px;margin:auto}.authenticator-option-label{text-align:right;width:200px;display:inline-block;padding:5px 10px 0 0}td .text-button{min-width:20px;margin:auto}.active-button-container{display:inline-block;min-width:28px}.edit-name-toolbar{display:inline-block;vertical-align:middle}@keyframes save-flash{from{opacity:0%}to{opacity:100%}}.data-grid-data-grid-node.centered{text-align:center}.data-grid td{vertical-align:middle}.credentials-title{display:block;font-weight:bold;margin:8px 0}.code{font-family:monospace}.learn-more{display:flex;justify-content:center;align-items:center;height:100%;text-align:center;overflow:hidden}.webauthn-pane.enabled .learn-more{display:none}\n/*# sourceURL=webauthnPane.css */\n");const s={export:"Export",remove:"Remove",noCredentialsTryCallingSFromYour:"No credentials. Try calling {PH1} from your website.",enableVirtualAuthenticator:"Enable virtual authenticator environment",id:"ID",isResident:"Is Resident",rpId:"RP ID",userHandle:"User Handle",signCount:"Signature Count",actions:"Actions",credentials:"Credentials",useWebauthnForPhishingresistant:"Use WebAuthn for phishing-resistant authentication",learnMore:"Learn more",newAuthenticator:"New authenticator",protocol:"Protocol",transport:"Transport",supportsResidentKeys:"Supports resident keys",supportsLargeBlob:"Supports large blob",add:"Add",addAuthenticator:"Add authenticator",active:"Active",editName:"Edit name",saveName:"Save name",authenticatorS:"Authenticator {PH1}",privateKeypem:"Private key.pem",uuid:"UUID",supportsUserVerification:"Supports user verification",yes:"Yes",no:"No",setSAsTheActiveAuthenticator:"Set {PH1} as the active authenticator"},l=i.i18n.registerUIStrings("panels/webauthn/WebauthnPane.ts",s),c=i.i18n.getLocalizedString.bind(void 0,l);class d extends o.DataGrid.DataGridNode{credential;constructor(t){super(t),this.credential=t}nodeSelfHeight(){return 24}createCell(t){const e=super.createCell(t);if(n.Tooltip.Tooltip.install(e,e.textContent||""),"actions"!==t)return e;const i=n.UIUtils.createTextButton(c(s.export),(()=>{this.dataGrid&&this.dataGrid.dispatchEventToListeners("ExportCredential",this.credential)}));e.appendChild(i);const a=n.UIUtils.createTextButton(c(s.remove),(()=>{this.dataGrid&&this.dataGrid.dispatchEventToListeners("RemoveCredential",this.credential)}));return e.appendChild(a),e}}class h extends o.DataGrid.DataGridImpl{}class u extends(t.ObjectWrapper.eventMixin(h)){}class b extends o.DataGrid.DataGridNode{createCells(t){t.removeChildren();const e=this.createTDWithClass(o.DataGrid.Align.Center);this.dataGrid&&(e.colSpan=this.dataGrid.visibleColumnsArray.length);const a=document.createElement("span",{is:"source-code"});a.textContent="navigator.credentials.create()",a.classList.add("code");const n=i.i18n.getFormatLocalizedString(l,s.noCredentialsTryCallingSFromYour,{PH1:a});e.appendChild(n),t.appendChild(e)}}let p;const m={Ctap2:"ctap2",U2f:"u2f"};class C extends n.Widget.VBox{#t=null;#e=!1;dataGrids=new Map;#i;#a;#o;#n;#r;#s;#l;#c;#d;#h;#u;#b;residentKeyCheckbox;#p;#m;#C;largeBlobCheckbox;addAuthenticatorButton;#v;constructor(){super(!0),a.TargetManager.TargetManager.instance().observeModels(a.WebAuthnModel.WebAuthnModel,this,{scoped:!0}),this.contentElement.classList.add("webauthn-pane"),this.#a=t.Settings.Settings.instance().createSetting("webauthnAuthenticators",[]),this.#g(),this.#n=this.contentElement.createChild("div","authenticators-view"),this.#x(),this.#A(!1)}static instance(t){return p&&!t?.forceNew||(p=new C),p}modelAdded(t){t.target()===t.target().outermostTarget()&&(this.#o=t)}modelRemoved(t){t.target()===t.target().outermostTarget()&&(this.#o=void 0)}async#k(){let t=null;const e=this.#a.get();for(const i of e){if(!this.#o)continue;const e=await this.#o.addAuthenticator(i);this.#f(e,i),i.authenticatorId=e,i.active&&(t=e)}this.#a.set(e),t&&this.#w(t)}async ownerViewDisposed(){this.#i&&this.#i.setChecked(!1),await this.#y(!1)}#g(){this.#r=this.contentElement.createChild("div","webauthn-toolbar-container"),this.#s=new n.Toolbar.Toolbar("webauthn-toolbar",this.#r);const t=c(s.enableVirtualAuthenticator);this.#i=new n.Toolbar.ToolbarCheckbox(t,t,this.#S.bind(this)),this.#s.appendToolbarItem(this.#i)}#E(t){const e=[{id:"credentialId",title:c(s.id),longText:!0,weight:24},{id:"isResidentCredential",title:c(s.isResident),dataType:o.DataGrid.DataType.Boolean,weight:10},{id:"rpId",title:c(s.rpId)},{id:"userHandle",title:c(s.userHandle)},{id:"signCount",title:c(s.signCount)},{id:"actions",title:c(s.actions)}],i={displayName:c(s.credentials),columns:e,editCallback:void 0,deleteCallback:void 0,refreshCallback:void 0},a=new u(i);return a.renderInline(),a.setStriped(!0),a.addEventListener("ExportCredential",this.#L,this),a.addEventListener("RemoveCredential",this.#I.bind(this,t)),a.rootNode().appendChild(new b),this.dataGrids.set(t,a),a}#L({data:t}){this.#T(t)}#I(t,{data:e}){this.#U(t,e.credentialId)}#B(t,{data:e}){const i=this.dataGrids.get(t);if(!i)return;const a=i.rootNode().children.find((t=>!Object.keys(t.data).length));a&&i.rootNode().removeChild(a);const o=new d(e.credential);i.rootNode().appendChild(o)}#V(t,{data:e}){const i=this.dataGrids.get(t);if(!i)return;const a=i.rootNode().children.find((t=>t.data?.credentialId===e.credential.credentialId));a&&(a.data=e.credential)}async#y(t){await this.#v,this.#v=new Promise((async i=>{t&&!this.#e&&(e.userMetrics.actionTaken(e.UserMetrics.Action.VirtualAuthenticatorEnvironmentEnabled),this.#e=!0),this.#o&&await this.#o.setVirtualAuthEnvEnabled(t),t?await this.#k():this.#N(),this.#A(t),this.#v=void 0,i()}))}#A(t){this.contentElement.classList.toggle("enabled",t)}#N(){this.#n.innerHTML="";for(const t of this.dataGrids.values())t.asWidget().detach();this.dataGrids.clear()}#S(t){this.#y(t.target.checked)}#K(t){if(!this.#u)return;const e=this.#u.value;this.#u.removeChildren();for(const e of t)this.#u.appendChild(new Option(e,e));this.#u.value=e,this.#u.value||(this.#u.selectedIndex=0)}#G(){this.#h&&this.residentKeyCheckbox&&this.#m&&this.largeBlobCheckbox&&("ctap2"===this.#h.value?(this.residentKeyCheckbox.disabled=!1,this.#m.disabled=!1,this.largeBlobCheckbox.disabled=!this.residentKeyCheckbox.checked,this.largeBlobCheckbox.disabled&&(this.largeBlobCheckbox.checked=!1),this.#K(["usb","ble","nfc","internal"])):(this.residentKeyCheckbox.checked=!1,this.residentKeyCheckbox.disabled=!0,this.#m.checked=!1,this.#m.disabled=!0,this.largeBlobCheckbox.checked=!1,this.largeBlobCheckbox.disabled=!0,this.#K(["usb","ble","nfc"])))}#x(){this.#l=this.contentElement.createChild("div","learn-more"),this.#l.appendChild(n.Fragment.html` <div> ${c(s.useWebauthnForPhishingresistant)}<br><br> ${n.XLink.XLink.create("https://developers.google.com/web/updates/2018/05/webauthn",c(s.learnMore))} </div> `),this.#c=this.contentElement.createChild("div","new-authenticator-container");const t=n.UIUtils.createLabel(c(s.newAuthenticator),"new-authenticator-title");this.#c.appendChild(t),this.#d=this.#c.createChild("div","new-authenticator-form");const e=this.#d.createChild("div","authenticator-option"),i=this.#d.createChild("div","authenticator-option"),a=this.#d.createChild("div","authenticator-option"),o=this.#d.createChild("div","authenticator-option"),r=this.#d.createChild("div","authenticator-option"),l=this.#d.createChild("div","authenticator-option"),d=n.UIUtils.createLabel(c(s.protocol),"authenticator-option-label");e.appendChild(d),this.#h=e.createChild("select","chrome-select"),n.ARIAUtils.bindLabelToControl(d,this.#h),Object.values(m).sort().forEach((t=>{this.#h&&this.#h.appendChild(new Option(t,t))})),this.#h&&(this.#h.value="ctap2");const h=n.UIUtils.createLabel(c(s.transport),"authenticator-option-label");i.appendChild(h),this.#u=i.createChild("select","chrome-select"),n.ARIAUtils.bindLabelToControl(h,this.#u),this.#b=n.UIUtils.CheckboxLabel.create(c(s.supportsResidentKeys),!1),this.#b.textElement.classList.add("authenticator-option-label"),a.appendChild(this.#b.textElement),this.residentKeyCheckbox=this.#b.checkboxElement,this.residentKeyCheckbox.checked=!1,this.residentKeyCheckbox.classList.add("authenticator-option-checkbox"),a.appendChild(this.#b),this.#p=n.UIUtils.CheckboxLabel.create(c(s.supportsUserVerification),!1),this.#p.textElement.classList.add("authenticator-option-label"),o.appendChild(this.#p.textElement),this.#m=this.#p.checkboxElement,this.#m.checked=!1,this.#m.classList.add("authenticator-option-checkbox"),o.appendChild(this.#p),this.#C=n.UIUtils.CheckboxLabel.create(c(s.supportsLargeBlob),!1),this.#C.textElement.classList.add("authenticator-option-label"),r.appendChild(this.#C.textElement),this.largeBlobCheckbox=this.#C.checkboxElement,this.largeBlobCheckbox.checked=!1,this.largeBlobCheckbox.classList.add("authenticator-option-checkbox"),this.largeBlobCheckbox.name="large-blob-checkbox",r.appendChild(this.#C),this.addAuthenticatorButton=n.UIUtils.createTextButton(c(s.add),this.#R.bind(this),""),l.createChild("div","authenticator-option-label"),l.appendChild(this.addAuthenticatorButton);const u=n.UIUtils.createLabel(c(s.addAuthenticator),"");n.ARIAUtils.bindLabelToControl(u,this.addAuthenticatorButton),this.#G(),this.#h&&this.#h.addEventListener("change",this.#G.bind(this)),this.residentKeyCheckbox&&this.residentKeyCheckbox.addEventListener("change",this.#G.bind(this))}async#R(){const t=this.#P();if(this.#o){const e=await this.#o.addAuthenticator(t),i=this.#a.get();i.push({authenticatorId:e,active:!0,...t}),this.#a.set(i.map((t=>({...t,active:t.authenticatorId===e}))));const a=await this.#f(e,t),o=window.matchMedia("(prefers-reduced-motion: reduce)").matches;a.scrollIntoView({block:"start",behavior:o?"auto":"smooth"})}}async#f(t,e){const i=document.createElement("div");i.classList.add("authenticator-section"),i.setAttribute("data-authenticator-id",t),this.#n.appendChild(i);const a=i.createChild("div","authenticator-section-header"),o=a.createChild("div","authenticator-section-title");n.ARIAUtils.markAsHeading(o,2),await this.#F();const r=a.createChild("div","active-button-container"),l=n.UIUtils.createRadioLabel(`active-authenticator-${t}`,c(s.active));l.radioElement.addEventListener("click",this.#w.bind(this,t)),r.appendChild(l),l.radioElement.checked=!0,this.#t=t;const d=a.createChild("button","text-button");d.textContent=c(s.remove),d.addEventListener("click",this.#D.bind(this,t));const h=new n.Toolbar.Toolbar("edit-name-toolbar",o),u=new n.Toolbar.ToolbarButton(c(s.editName),"edit"),b=new n.Toolbar.ToolbarButton(c(s.saveName),"checkmark");b.setVisible(!1);const p=o.createChild("input","authenticator-name-field");p.disabled=!0;const m=t.slice(-5);p.value=c(s.authenticatorS,{PH1:m}),this.#M(l,p.value),u.addEventListener(n.Toolbar.ToolbarButton.Events.Click,(()=>this.#O(o,p,u,b))),b.addEventListener(n.Toolbar.ToolbarButton.Events.Click,(()=>this.#H(o,p,u,b,l))),p.addEventListener("focusout",(()=>this.#H(o,p,u,b,l))),p.addEventListener("keydown",(t=>{"Enter"===t.key&&this.#H(o,p,u,b,l)})),h.appendToolbarItem(u),h.appendToolbarItem(b),this.#W(i,t,e);const C=document.createElement("div");C.classList.add("credentials-title"),C.textContent=c(s.credentials),i.appendChild(C);return this.#E(t).asWidget().show(i),this.#o&&(this.#o.addEventListener("CredentialAdded",this.#B.bind(this,t)),this.#o.addEventListener("CredentialAsserted",this.#V.bind(this,t))),i}#T(t){let e="-----BEGIN PRIVATE KEY-----\n";for(let i=0;i<t.privateKey.length;i+=64)e+=t.privateKey.substring(i,i+64)+"\n";e+="-----END PRIVATE KEY-----";const i=document.createElement("a");i.download=c(s.privateKeypem),i.href="data:application/x-pem-file,"+encodeURIComponent(e),i.click()}async#U(t,e){const i=this.dataGrids.get(t);i&&(i.rootNode().children.find((t=>t.data.credentialId===e)).remove(),i.rootNode().children.length||i.rootNode().appendChild(new b),this.#o&&await this.#o.removeCredential(t,e))}#W(t,e,i){const a=t.createChild("div","authenticator-fields"),o=a.createChild("div","authenticator-field"),r=a.createChild("div","authenticator-field"),l=a.createChild("div","authenticator-field"),d=a.createChild("div","authenticator-field"),h=a.createChild("div","authenticator-field"),u=a.createChild("div","authenticator-field");o.appendChild(n.UIUtils.createLabel(c(s.uuid),"authenticator-option-label")),r.appendChild(n.UIUtils.createLabel(c(s.protocol),"authenticator-option-label")),l.appendChild(n.UIUtils.createLabel(c(s.transport),"authenticator-option-label")),d.appendChild(n.UIUtils.createLabel(c(s.supportsResidentKeys),"authenticator-option-label")),h.appendChild(n.UIUtils.createLabel(c(s.supportsLargeBlob),"authenticator-option-label")),u.appendChild(n.UIUtils.createLabel(c(s.supportsUserVerification),"authenticator-option-label")),o.createChild("div","authenticator-field-value").textContent=e,r.createChild("div","authenticator-field-value").textContent=i.protocol,l.createChild("div","authenticator-field-value").textContent=i.transport,d.createChild("div","authenticator-field-value").textContent=i.hasResidentKey?c(s.yes):c(s.no),h.createChild("div","authenticator-field-value").textContent=i.hasLargeBlob?c(s.yes):c(s.no),u.createChild("div","authenticator-field-value").textContent=i.hasUserVerification?c(s.yes):c(s.no)}#O(t,e,i,a){e.disabled=!1,t.classList.add("editing-name"),e.focus(),a.setVisible(!0),i.setVisible(!1)}#H(t,e,i,a,o){e.disabled=!0,t.classList.remove("editing-name"),i.setVisible(!0),a.setVisible(!1),this.#M(o,e.value)}#M(t,e){n.Tooltip.Tooltip.install(t.radioElement,c(s.setSAsTheActiveAuthenticator,{PH1:e}))}#D(t){if(this.#n){const e=this.#n.querySelector(`[data-authenticator-id=${CSS.escape(t)}]`);e&&e.remove()}const e=this.dataGrids.get(t);e&&(e.asWidget().detach(),this.dataGrids.delete(t)),this.#o&&this.#o.removeAuthenticator(t);const i=this.#a.get().filter((e=>e.authenticatorId!==t));if(this.#a.set(i),this.#t===t){const t=Array.from(this.dataGrids.keys());t.length?this.#w(t[0]):this.#t=null}}#P(){if(!(this.#h&&this.#u&&this.residentKeyCheckbox&&this.#m&&this.largeBlobCheckbox))throw new Error("Unable to create options from current inputs");return{protocol:this.#h.options[this.#h.selectedIndex].value,ctap2Version:"ctap2_1",transport:this.#u.options[this.#u.selectedIndex].value,hasResidentKey:this.residentKeyCheckbox.checked,hasUserVerification:this.#m.checked,hasLargeBlob:this.largeBlobCheckbox.checked,automaticPresenceSimulation:!0,isUserVerified:!0}}async#w(t){await this.#F(),this.#o&&await this.#o.setAutomaticPresenceSimulation(t,!0),this.#t=t;const e=this.#a.get().map((e=>({...e,active:e.authenticatorId===t})));this.#a.set(e),this.#j()}#j(){const t=this.#n.getElementsByClassName("authenticator-section");Array.from(t).forEach((t=>{const e=t.querySelector("input.dt-radio-button");e&&(e.checked=t.dataset.authenticatorId===this.#t)}))}async#F(){this.#t&&this.#o&&await this.#o.setAutomaticPresenceSimulation(this.#t,!1),this.#t=null,this.#j()}wasShown(){super.wasShown(),this.registerCSSFiles([r])}}var v=Object.freeze({__proto__:null,WebauthnPaneImpl:C});export{v as WebauthnPane};
